<?php

class User extends Controller {
	
	function User()
	{
		//Constructor 
		parent::Controller();
		$this->load->model('usermodel');
		$this->load->model('wallmodel');
		$this->load->model('friendsmodel');
		$this->load->library('form_validation');
		$this->load->model('photomodel');
		$this->load->library('email');
	}

	function id()
	{
		$userId = $this->uri->segment(3);

		$username = $this->usermodel->getName($userId);
		if($username == false)
		{
			$this->session->set_flashdata('error',  'ขอโทษด้วย! ระบบไม่สามารถทำตามที่คุณขอได้ ซึ่งปัญหาที่เกิดอาจมีหลายสาเหตุดังนี้ <br />1.คุณกำลังกรอก URL เข้ามาโดยตรงซึ่งเราไม่พบไอดีที่คุณต้องการ <br />2.คุณยังไม่ได้ล๊อกอินเข้าสู่ระบบ.');
			redirect('user/error');
		}
		else if($userId == $this->session->userdata('userId'))
		{
			redirect('profile');
		}
		else
		{
			
			$data['titlebar'] = 'Panoramap.in.th | '.$username;
			$data['sessUserId'] = $this->session->userdata('userId');//Get Session Data
			$data['friends'] = $this->usermodel->getFriends($userId);
			$data['username'] = $this->usermodel->getName($userId);
			$data['avatar'] = $this->usermodel->getAvatar($userId);
			$data['email'] = $this->usermodel->getEmail($userId);
			$data['lastlogin'] = $this->usermodel->getLastlogin($userId);
			$data['lat'] = $this->usermodel->getLat($userId);
			$data['lng'] = $this->usermodel->getLng($userId);
			$data['uid'] = $userId;
			//$data['wallupdate'] = $this->wallmodel->wallUpdate($userId);
			$data['wall'] = $this->wallmodel->getWall($userId);
			$data['about'] = $this->usermodel->getAbout($userId);
			$data['album'] = $this->photomodel->getUserAlbum($userId, 'datetime desc', '0', '20');
			
			if($this->session->userdata('userId') != '')
			{
				$data['isFriendConfirmed'] = $this->friendsmodel->isConfirmed($this->session->userdata('userId'), $userId);
				$data['isFriendWaitConfirmed'] = $this->friendsmodel->isWaitConfirmed($this->session->userdata('userId'), $userId);
			}
			else
			{
				$data['isFriendConfirmed'] = $this->friendsmodel->isConfirmed($userId, $userId);
				$data['isFriendWaitConfirmed'] = $this->friendsmodel->isWaitConfirmed($userId, $userId);
			}

			$this->load->view('user_view', $data);
		}
	}
	
	function wallPost()
	{
		loginRequired('login');
		
		$userId = $this->uri->segment(3);
		$this->load->model('wallmodel');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('wallpost', 'Wall Post', 'trim|required|xss_clean');

		if($this->form_validation->run() == FALSE)
		{
			//$this->session->set_flashdata('error',  'คุณยังไม่ได้กรอกข้อความ.');
			redirect('user/id/' . $userId);	
		}
		else
		{
			$normal = 1;
			$action = 2;
			
			$sessUserid = $this->session->userdata('userId');
			$this->wallmodel->wallPost($sessUserid, $userId, $this->input->post('wallpost'), $normal);
			
			$am = $this->usermodel->getName($this->session->userdata('userId'));
			$you = $this->usermodel->getName($userId);
			//$data = "<img src='".base_url()."application/images/note.gif' /><i>" . $am . " เขียนบนกระดานข้อความของ ".anchor('user/id/'.$userId, $you) ."</i>";
			//$this->wallmodel->wallPost($sessUserid, $sessUserid, $data, $action);
			
			$link = base_url() . 'user/id/' . $userId;
			$email = $this->usermodel->getEmail($userId);

			$config['mailtype'] = 'html';
			$this->email->initialize($config);
			$this->email->from('no-reply@panoramap.in.th', 'Panoramap');
			$this->email->to($email);
			$this->email->subject('ข้อความใหม่');
			$this->email->message($am.' ได้เขียนข้อความบนข้อความบนกระดานของคุณว่า: <br /><p> \" ' . $this->input->post('wallpost'). ' \"</p> ' . ' 
			<br />ดูข้อความของคุณ: ' . $link);
			$this->email->send();

			redirect('user/id/' . $userId);
		}
	}
	
	function subWallPost()
	{
		loginRequired('login');
	
		$userId = $this->uri->segment(3);
		$this->load->model('wallmodel');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('comment', 'Comment Post', 'trim|required|xss_clean');

		if($this->form_validation->run() == FALSE)
		{

			//$this->session->set_flashdata('error',  'คุณยังไม่ได้กรอกข้อความ.');
			redirect('user/id/' . $userId);	
		}
		else
		{
			$ref_wall = $this->input->post('subWallId');
			$userid = $this->session->userdata('userId');
			$this->wallmodel->subWallPost($userid, $ref_wall, $this->input->post('comment'));

			$am = $this->usermodel->getName($this->session->userdata('userId'));
			$link = base_url() . 'user/id/' . $userId;
			$email = $this->usermodel->getEmail($userId);

			$config['mailtype'] = 'html';
			$this->email->initialize($config);
			$this->email->from('no-reply@panoramap.in.th', 'Panoramap');
			$this->email->to($email);
			$this->email->subject('ข้อความโต้ตอบใหม่');
			$this->email->message($am.' ได้โต้ตอบบนข้อความบนกระดานของคุณว่า: <br /><p> \" ' . $this->input->post('comment'). ' \"</p> ' . ' 
			<br />ดูข้อความของคุณ: ' . $link);
			$this->email->send();

			redirect('user/id/' . $userId);	
		}
	}
	
	function addFriend()
	{
		loginRequired('login');
		
		$userid = $this->session->userdata('userId');
		$am = $this->usermodel->getName($this->session->userdata('userId'));
		$link = base_url() . 'profile/notifications';

		$this->load->model('friendsmodel');
		$friendId = $this->uri->segment(3);
		$friendEmail = $this->uri->segment(4);
		$this->friendsmodel->newInvitation($friendEmail, $friendId);

		$config['mailtype'] = 'html';
		$this->email->initialize($config);
		$this->email->from('no-reply@panoramap.in.th', 'Panoramap');
		$this->email->to($friendEmail);
		$this->email->subject('คำร้องขอติดตาม');
		$this->email->message($am.' ต้องการเป็นเพื่อน และติดตามโปรไฟล์ของคุณ <br />ดูคำร้องทั้งหมดของคุณ: ' . $link);
		$this->email->send();

		redirect('user/id/'.$friendId);
	}
	
	function cancelFriend()
	{
		loginRequired('login');
		
		$this->load->model('friendsmodel');
		$friendId = $this->uri->segment(3);
		$this->friendsmodel->cancelFriend($friendId);
		redirect('user/id/'.$friendId);
	}
	
	function album()
	{
		$userId = $this->uri->segment(3);
		$data['album'] = $this->photomodel->getUserAlbum($userId);
		$data['titlebar'] = "Panoramap.in.th | อัลบั้ม";
		$this->load->view('user_album_view', $data);
	}
	
	function error()
	{
		$data['titlebar'] = "Panoramap.in.th - อุ๊ปส์! คุณมีปัญหาแล้วละ.";
		$this->load->view('user_error_view', $data);
	}

}